package com.yamanyar.weebill.domain;

// Generated Jan 31, 2008 6:26:41 PM by Hibernate Tools 3.2.0.CR1

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import org.hibernate.validator.NotNull;

/**
 * <code>Component</code> is a part of a <code>Project</code>.
 * <code>Project</code> has zero or more <code>Component</code>s. But a
 * <code>Component</code> can only have one <code>Project</code>. Bugs or
 * tasks then can be assigned to <code>Component</code> or
 * <code>Project</code> for a better organization.
 * 
 * @author Kaan Yamanyar
 */
@Entity
public class Component implements java.io.Serializable {

	private static final long serialVersionUID = 5922660694896356641L;

	private Long id;

	private Project project;

	private String name;

	private String description;

	/**
	 * @return Description of the <code>Compoenent</code> if exists. Maximum
	 *         allowed length is 400 chars.
	 */
	@Column(length = 400)
	@NotNull
	public String getDescription() {
		return this.description;
	}

	/**
	 * 
	 * @param description
	 *            Description for the <code>Compoenent</code>. Maximum
	 *            allowed length is 400 chars.
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Returns compoenent's name. If loaded from db, it is not supoosed to be
	 * null.
	 * 
	 * @return Compoenet's name. Max 50 chars allowed.
	 */
	@NotNull
	@Column(length = 50)
	public String getName() {
		return this.name;
	}

	/**
	 * 
	 * @param name
	 *            Name for the compoenent.Max 50 chars allowed.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 
	 * @return The project which this component is owned by.
	 * @see Project#getComponents()
	 */
	@ManyToOne
	@JoinColumn(name = "projectId")
	// projectId will be created in component object's table
	@NotNull
	public Project getProject() {
		return this.project;
	}

	/**
	 * 
	 * @param project
	 *            The project which this component is owned by.
	 * @see Project#setComponents(java.util.Set)
	 */
	public void setProject(Project project) {
		this.project = project;
	}

	/**
	 * 
	 * @return The id of component. Id is auto-generated by db.
	 */
	@Id
	@GeneratedValue
	public Long getId() {
		return this.id;
	}

	/**
	 * Overridden in order to display the name of the compoenent.
	 */
	@Override
	public String toString() {
		return this.name;
	}

	/*
	 * There might be multiple component with a same name.
	 * 
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof Component)) {
			return false;
		}
		Component that = (Component) obj;
		return this.id.equals(that.getId());
	}

	@Override
	public int hashCode() {
		if (this.id == null) {
			return 0;
		}
		return (int) (this.id / 10) + 1;
	}

	/**
	 * This method is not supposed to be called by a developer.
	 * 
	 * @param id
	 */
	public void setId(Long id) {
		this.id = id;
	}

}